package com.example.exam.sysPermission.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.sysPermission.entity.SysPermissionBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class SysPermissionDao extends BaseDao<SysPermissionBean, Integer> {
	@Override
	    /**
	        * @Description:查询一条角色信息SQL语句
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String findByIdSql() {
		return "select * from sys_role where id = ?";
	}
	@Override
	    /**
	        * @Description:添加权限信息SQL语句
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getAddSql() {
		return "insert into sys_permission (id, name, coding, resource_id, create_time, create_user, state) values (:id, :name, :coding, :resourceId, :createTime, :createUser, :state) ";
	}

	@Override
	    /**
	        * @Description:修改权限信息SQL语句
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getUpdateAllFieldSql() {
		return "update sys_permission set name=:name, coding=:coding, resource_id=:resourceId, create_time=:createTime, create_user=:createUser, state=:state where id=:id ";
	}

	@Override
	    /**
	        * @Description:修改权限状态SQL语句逻辑删除
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getRemoveByIdSql() {
		return "update sys_permission set state = -1 where id = ? ";
	}
	/**
	 * @Description:删除权限信息物理删除
	 * @Param: [id]
	 * @return: java.lang.Integer
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */ public Integer deletePer(Integer id) {
		String sql="delete from sys_permission where id =?";
		return getJtl().update(sql,id);
	}
	/**
	 * @Description:查询对应菜单的权限集
	 * @Param: [resourceId]
	 * @return: java.util.List<com.example.exam.sysPermission.entity.SysPermissionBean>
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public List<SysPermissionBean> queryPermissionList(Integer resourceId) {
		List<Object> list = new ArrayList<Object>();
		String sql = "SELECT * FROM sys_permission WHERE state=1";
		if (resourceId != null) {
			sql += " AND FIND_IN_SET(resource_id,?)";
			list.add(resourceId);
		}
		return getJtl().query(sql, list.toArray(),
				new BeanPropertyRowMapper<SysPermissionBean>(SysPermissionBean.class));
	}
	/**
	 * @Description:
	 * @Param: [resourceId, coding, state]
	 * @return: int
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public int upPermissionByResourceId(Integer resourceId, String coding, Integer state) {
		try {
			List<Object> list = new ArrayList<Object>();
			String sql = " update sys_permission set state = ? where 1=1 ";
			list.add(state);
			if (!StringUtils.isEmpty(coding)) {
				sql += " and coding=?";
				list.add(coding);
			}
			if (resourceId != null) {
				sql += " and resource_id=?";
				list.add(resourceId);
			}
			return getJtl().update(sql, list.toArray());
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}
	/**
	 * @Description:
	 * @Param: [coding, resourceId]
	 * @return: com.example.exam.sysPermission.entity.SysPermissionBean
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public SysPermissionBean getPermission(String coding, Integer resourceId) {
		try {
			List<Object> list = new ArrayList<Object>();
			String sql = "SELECT * FROM sys_permission WHERE 1 = 1";
			if (!StringUtils.isEmpty(coding)) {
				sql += " AND coding=?";
				list.add(coding);
			}
			if (resourceId != null) {
				sql += " AND resource_id=?";
				list.add(resourceId);
			}
			return getJtl().queryForObject(sql, list.toArray(),
					new BeanPropertyRowMapper<SysPermissionBean>(SysPermissionBean.class));
		} catch (DataAccessException e) {
			return null;
		}
	}

}